package com.example.eurekaconsumer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: 静态资源配置
 * @projectName spring-cloud-demo
 * @Date: 2021/3/4 16:24
 * @Author: lei.yu-esint
 * @Description:
 */

@Configuration
public class WebAppConfigurer extends WebMvcConfigurationSupport {
    private List<String> excludePathPatternList;

    /**
     * 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
     *
     * @param registry 注册管理
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginStatusFilter()).addPathPatterns("/**")
                .excludePathPatterns(getUrls());
        super.addInterceptors(registry);
    }


    /**
     * 这个方法是用来配置静态资源的，比如html，js，css，等等
     *
     * @param registry 注册管理
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    /**
     * 定义需要避免过滤的url
     *
     * @return list
     */
    private List<String> getUrls() {
        List<String> list = new ArrayList<>();
        list.add("/user/login");
        list.add("/user/doLogin");
        list.add("/static/**");
        list.add("/consumer/**");
        list.add("/consumer/css/*.css");
        list.add("/consumer/js/*.js");
        list.add("/consumer/lib/*");
        list.add("/consumer/images/*");
        this.setExcludePathPatternList(list);
        return excludePathPatternList;
    }

    public void setExcludePathPatternList(List<String> excludePathPatternList) {
        this.excludePathPatternList = excludePathPatternList;
    }
}
